﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using QLNHASACH.DTO;
using System.Windows.Forms;
using QLNHASACH.BussinessLayer;
//tam
using QLNHASACH.DataAccessLayer;
using QLNHASACH.BussinessLayer.ObjectBUS;

namespace QLNHASACH.Presentation
{
    public partial class SachCtrl : UserControl
    {
        public SachCtrl()
        {
            InitializeComponent();
        }
        SachDTO sach =new SachDTO();
        DauSachDTO dauSach =new DauSachDTO();
        bool themSua;//Neu them =1 Sua =0
        #region Cac ham chuc nang
        //Khoa cac control
        public void EnableEditing(bool editting)
        {
            //button
            btSua.Enabled = !editting;
            btXoa.Enabled = !editting;
            btLuu.Enabled = editting;
            btKhongLuu.Enabled = editting;
            //textbox, cb
            txbTenSach.Enabled = editting;
            cbTacGia.Enabled = editting;
            cbTheLoai.Enabled = editting;
            cbNhaXuatBan.Enabled = editting;
            txbNamXuatBan.Enabled = editting;
            txbNamXuatBan.Enabled = editting;
        }
        //reset gia tri
        public void ResetValuse()
        {
            txbTenSach.Text = "";
            cbTacGia.Text = "";
            cbTheLoai.Text = "";
            cbNhaXuatBan.Text = "";
            txbNamXuatBan.Text = "";
        }
        #endregion
        private void NhapSachCtrl_Load(object sender, EventArgs e)
        {
            //Load dataGridview
            gridEXSach.DataSource = SachBUS.LayDSSach();
            //Load DanhSach Tac Gia
            cbTacGia.DataSource = SachBUS.LayDSTacGia();
            cbTacGia.DisplayMember = "TacGia";
            cbTacGia.ValueMember = "TacGia";
            //Load Danh Sach The Loai
            cbTheLoai.DataSource = TheLoaiBUS.LayDSTheLoai();
            cbTheLoai.DisplayMember = "TenTheLoai";
            cbTheLoai.ValueMember = "MaTheLoai";
            //Load Danh Sach Nha Xuat Ban
            cbNhaXuatBan.DataSource = SachBUS.LayDSNhaXuatBan();
            cbNhaXuatBan.DisplayMember = "NhaXuatBan";
            cbNhaXuatBan.ValueMember = "NhaXuatBan";
            //Khoa control
            EnableEditing(false);
            //tim kiem
            rbTenSach.Checked = true;          
        }

        private void gridEXSach_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                txbTenSach.Text = gridEXSach.CurrentRow.Cells[1].Value.ToString();
                cbTheLoai.Text = gridEXSach.CurrentRow.Cells[2].Value.ToString();
                cbTacGia.Text = gridEXSach.CurrentRow.Cells[3].Value.ToString();
                cbNhaXuatBan.Text = gridEXSach.CurrentRow.Cells[4].Value.ToString();
                txbNamXuatBan.Text = gridEXSach.CurrentRow.Cells[5].Value.ToString();
            }
            catch (Exception)
            {
            } 
        }
       
        private void btThem_Click(object sender, EventArgs e)
        {
            themSua = true;
            ResetValuse();
            EnableEditing(true); 
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            themSua = false;
            EnableEditing(true);
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa sách " + txbTenSach.Text + " không.?", "Hỏi",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                LayThongTinDauSach();
                LayThongTinSach();
                if (SachBUS.XoaMotSach(sach) == true)
                    MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (SachBUS.KTMotDauSach(sach) == false)
                {
                    DauSachBUS.XoaMotDauSach(dauSach);
                }
            }
            NhapSachCtrl_Load(sender, e);
        }
        private void LayThongTinSach()
        { 
            //Thông tin sách
            sach.MaSach = int.Parse(gridEXSach.CurrentRow.Cells[0].Value.ToString());
            sach.MaDauSach = DauSachBUS.LayMaDauSachTheoTen(txbTenSach.Text);
            sach.NhaXuatBan = cbNhaXuatBan.Text;
            sach.NamXuatBan = int.Parse(txbNamXuatBan.Text);
            sach.MaSach = int.Parse(gridEXSach.CurrentRow.Cells[0].Value.ToString());
            dauSach.MaDauSach = sach.MaDauSach; 
                   
        }
        private void LayThongTinDauSach()
        {
            dauSach.TenDauSach = txbTenSach.Text;
            dauSach.TacGia = cbTacGia.Text;
            dauSach.MaTheLoai = TheLoaiBUS.LayMaTheLoaiTheoTen(cbTheLoai.Text);
        }
        private void btLuu_Click(object sender, EventArgs e)
        {
            //Tự thêm thể loại nếu có thể loại mới
            TheLoaiBUS.ThemTheLoai(cbTheLoai.Text);

            
            //Thông tin đầu sách
            LayThongTinDauSach();
            //Tự động thêm đầu sách nếu có đầu sách mới
            if (DauSachBUS.ThemDauSach(dauSach) == false)
            {
                DauSachBUS.CapNhatDauSach(dauSach);
            }
            //Lấy thông tin
            LayThongTinSach();
            //Thêm sách
            if (themSua==true && SachBUS.ThemSach(sach) == true)
            {
                MessageBox.Show("Thêm sách thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //Cập nhật sách
            if (themSua == false && SachBUS.CapNhatSach(sach) == true)
            {
                MessageBox.Show("Cập nhật sách thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            NhapSachCtrl_Load(sender, e);
        }
        private void btKhongLuu_Click(object sender, EventArgs e)
        {
            EnableEditing(false);
        }
        private void txbTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (txbTimKiem.Text != "")
            {
                if (rbTenSach.Checked)
                    gridEXSach.DataSource = SachBUS.TimTheoTen(txbTimKiem.Text);
                if (rbTacGia.Checked)
                    gridEXSach.DataSource = SachBUS.TimTheoTacGia(txbTimKiem.Text);
                if (rbTheLoai.Checked)
                    gridEXSach.DataSource = SachBUS.TimTheoTheLoai(txbTimKiem.Text);
                if (rbNhaXuaBan.Checked)
                    gridEXSach.DataSource = SachBUS.TimTheoNhaXuatBan(txbTimKiem.Text);   
            }
            else gridEXSach.DataSource = SachBUS.LayDSSach();
        }
    }
}
